#include "vtor_string_player.h"

// 修改成用户的工程宏
#ifdef __DEMO_PROJECT__
// 加入用户用中需要用到的头文件
#include "main.h"
//#include "tim.h"
#include "vtor_elec_module.h"


// 实例化一个蜂鸣器音乐播放器

VtorStringPlayer vtorStringPlayer[2];
VtorStringPlayer* beepPlayer;
VtorStringPlayer* beepRecordPlayer;
char recordPushIdx = 0;
char recordString[512] = {0};

//0不记录 1开启记录
int devRecordMode = 0;

void Beep_SetFreq(int freq)
{
	// 使用定时器中断方式
	if(0 == freq)
	{
		//HAL_TIM_Base_Stop(&htim2);
	}
	else
	{
		/*
		arr值1 1us中断 1mhz中断 500khz音调
		arr值2 2us中断 500khz中断 250khz音调
		音调 = 1000000/2/arr
		*/
		//htim2.Instance->ARR = 1000000 / 2 / freq;
		//if(htim2.State == HAL_TIM_STATE_READY)
		{
		//	HAL_TIM_Base_Start_IT(&htim2);
		}
	}
	
	// 使用pwm方式
	// prescale = 1 - 1
	// arr = 72 - 1（此定时器时钟为72mhz情况下）
	if(freq != 0)
	{
		HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3); // 喇叭
		__HAL_TIM_PRESCALER(&htim3, 1000000 / freq - 1);
	}
	else
	{
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);
	}
}

const uint8_t xiaoxingxing[]={

	// 小星星
	"10.10.50.50.60.60.50="
	"40.40.30.30.20.20.10="
	"50.50.40.40.30.30.20="
	"50.50.40.40.30.30.20="
	"10.10.50.50.60.60.50="
	"40.40.30.30.20.20.10==",
};
const uint8_t yujian[]={

	// 孙燕姿-遇见
	"567+1=;07+17656=;05=;0="
	"1234=:0,345123==03=0="
	"567+1=;0,7+1+2+1+2+3==05=;0="
	"1234=:,0:343221-71=;0=="
};

int16_t recordMusicBufIdx = 0;
uint8_t recordMusicBuf[500] = {"00000000000"};
uint8_t* recordMusicStr = recordMusicBuf;

const uint8_t* stringMusicArray[] = {
	xiaoxingxing,
	yujian,
	recordMusicBuf,
	NULL
};


const uint8_t* recordMusicArray[] = {
	recordMusicBuf,
	NULL
};

void VtorStringPlayer_Init()
{
	beepPlayer = &vtorStringPlayer[0];
	beepRecordPlayer = &vtorStringPlayer[1];

	VtorStringPlayer* player = beepPlayer;
	player->musicList = stringMusicArray;
	player->curMusic = player->musicList;
	player->curMusicChar = player->curMusic[0];
	player->presetToneLen = 50;

	player = beepRecordPlayer;
	player->musicList = recordMusicArray;
	player->curMusic = player->musicList;
	player->curMusicChar = player->curMusic[0];
	player->presetToneLen = 50;
}

// 切换歌曲的回调函数
void VtorStringPlayer_PlayFreq(VtorStringPlayer* player)
{
	if(player == beepPlayer)
	{
		Beep_SetFreq(player->freq);
	}
	if(player == beepRecordPlayer)
	{
		Beep_SetFreq(player->freq);
	}
}

void VtorTimer_PianoTask()
{
	static uint8_t oldToneIdx = 0;
	static uint8_t oldToneLen = 0;
	// PB 3~9
	uint8_t toneIdx = 0;
	uint8_t toneKey = (GPIOB->IDR & 0x3f8) >> 3;
	for(int i = 0; i < 7; i++)
	{
		if(0 == (toneKey & (1 << i)))
		{
			toneIdx = i + 7 + 1;
			break;
		}
	}
	if(toneIdx != 0)
	{
		if(0 == HAL_GPIO_ReadPin(LOW_TONE_BTN_GPIO_Port, LOW_TONE_BTN_Pin))
		{
			toneIdx -= 7;
		}
		if(0 == HAL_GPIO_ReadPin(HIGH_TONE_BTN_GPIO_Port, HIGH_TONE_BTN_Pin))
		{
			toneIdx += 7;
		}
	}
	int16_t freq = VtorStringPlayer_toneArray[toneIdx];
	if(devRecordMode)
	{
		oldToneLen++;
		if(oldToneIdx != toneIdx)
		{
			recordMusicStr = VtorStringPlayer_PushTone(recordMusicStr, oldToneIdx, oldToneLen);
			//VtorDebugChanPrintf("%d %d\r\n", oldToneIdx, oldToneLen);
			//VtorDebugChanPrintf("%s\r\n", recordMusicBuf);
			oldToneLen = 0;
			oldToneIdx = toneIdx;
		}
	}
	Beep_SetFreq(freq);
}

void VtorStringPlayer_SwitchMusicCallback(VtorStringPlayer* player)
{
	// 可以单曲循环，也可以全部循环
	player->curMusic++;
	if(NULL == *player->curMusic)
	{
		player->curMusic = player->musicList;
	}
//	printf("music %s\r\n",*player->curMusic[0]);
/*
	if(*player->curMusic == stringMusic_HuYanBing_YueGuang)
	{
		//player->presetToneLen = 4;
	}
*/
}

#endif

